package com.net128.beatportapps;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/*
 * http://www.beatport.com/en-US/xml/user/downloads/downloaded?pageNumber=1&resultsPerPage=300
 */
public class TrackNamer {
	private class TrackIdMapping {
		private String trackId;
		private String trackName;
		public TrackIdMapping(String trackId, String trackName) {
			super();
			this.trackId = trackId;
			this.trackName = trackName;
		}
		public String getTrackId() {
			return trackId;
		}
		public String getTrackName() {
			return trackName;
		}
		
		public String toString() {
			return trackId+"\t"+trackName;
		}
	}
	
	private String getYear(String date) {
		int pos=date.indexOf('-');
		if(pos>=0) {
			date=date.substring(0,pos);
		}
		return date;
	}
	
	private TrackIdMapping [] getTrackIdMappings(URL trackXml) throws Exception {
		DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docbuilder = docfactory.newDocumentBuilder();
		Document docroot = docbuilder.parse(trackXml.openStream());
		XPath xpath = XPathFactory.newInstance().newXPath();
		NodeList trackList = (NodeList) xpath.evaluate("//track", docroot, XPathConstants.NODESET);
		
		int n=trackList.getLength();
		TrackIdMapping[] trackIdMappings=new TrackIdMapping[n];
		for(int i=0;i<n;i++) {
			Element track = (Element) trackList.item(i);
			String id=track.getAttribute("id");//context.getValue("//track");
			String name=(String)xpath.evaluate("name", track, XPathConstants.STRING);
			String yearAdded=getYear((String)xpath.evaluate("dateAdded", track, XPathConstants.STRING));
			String yearReleased=getYear((String)xpath.evaluate("dateReleased", track, XPathConstants.STRING));
			String mixName=(String)xpath.evaluate("mixName", track, XPathConstants.STRING);
			if("Original Mix".equals(mixName)) {
				mixName="";
			} else if(mixName.length()>0){
				mixName=" ("+mixName+")";
			}
			mixName=mixName.replaceAll(", ", "+");
			String labelName=(String)xpath.evaluate("label/name", track, XPathConstants.STRING);
			String artistName=(String)xpath.evaluate("performer[@ref='Artist']/name", track, XPathConstants.STRING);
			String trackName=yearAdded+"__www_beatport_com ("+labelName+")__"+yearReleased+"__"+
				artistName+" - "+name+mixName;
			trackName=trackName.replace("&", "+");
			trackName=trackName.replaceAll("[.?]", "");
			trackName=trackName.replaceAll("__www_beatport_com", "__www.beatport.com");
			trackName=trackName.replaceAll(" [+] ", "+");
			trackName=trackName.replaceAll("\\(INACTIVE\\)","");
			trackName=trackName.toLowerCase();
			trackName=trackName.replaceAll("remix", "rmx");
			trackIdMappings[i]=new TrackIdMapping(id,trackName);
			System.out.println(trackIdMappings[i]+".wav");
		}
		return trackIdMappings;
	}
	
	public void run(String [] args) throws Exception {
		for(int i=1;i<=4;i++) {
			URL trackXml = getClass().getResource("downloaded"+i+".xml");
			getTrackIdMappings(trackXml);
		}
	}
	
	public static void main(String args[]) throws Exception {
		new TrackNamer().run(args);
	}
}
